<script setup lang="ts">
import { ref, onMounted } from 'vue'
import useUser  from '@/hooks/acl/useUser'
let formRef: any = ref('formRef') // 抽屉表单对象
const {
    tableData,
    useradd_data,
    dialog,
    username,
    currentPage4,
    pageSize4,
    total4,
    rules,
    role_data,
    dialog_role,
    roleNames,
    checkAll,
    isIndeterminate,
    selectedRoles,
    getHasUser,
    handler,
    handleSearch,
    handleReset,
    handleRole,
    handleEdit,
    handleDelete,
    handleAdd,
    handleDeleteAll,
    getUsers,
    handleAddsubmit,
    handleCheckAllChange,
    handleCheckedCitiesChange,
    submitRole
} = useUser()

onMounted(() => {
    getUsers();
})
// 提交用户
async function handleUser() {
    await formRef.value.validate();
    handleAddsubmit();
}



</script>

<template>
    <el-card>
        <el-form class="form">
            <el-form-item label="用户名:">
                <el-input v-model="username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleSearch">搜索</el-button>
                <el-button type="primary" @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card>
        <template #header>
            <el-button type="primary" @click="handleAdd">添加用户</el-button>
            <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>
        </template>
        <el-scrollbar style="height: calc(100vh - 350px);"> <!-- 菜单滚动条 -->
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="selection" align="center"></el-table-column>
                <el-table-column type="index" label="序号"></el-table-column>
                <el-table-column prop="UserID" label="用户ID"></el-table-column>
                <el-table-column prop="UserNumber" label="账号"></el-table-column>
                <el-table-column prop="UserName" label="用户姓名"></el-table-column>
                <el-table-column prop="roles" label="用户角色"></el-table-column>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button size="small" type="success" @click="handleRole(scope.row)">分配角色</el-button>
                        <el-button size="small" type="primary" @click="handleEdit()">编辑</el-button>
                        <el-popconfirm width="220" confirm-button-text="OK" cancel-button-text="No, Thanks"
                            icon="InfoFilled" icon-color="#626AEF" title="是否确认删除该用户?"
                            @confirm="handleDelete(scope.row.UserID)">
                            <template #reference>
                                <el-button type="danger" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </el-scrollbar>
        <footer style="margin: 10px 0;">
            <!-- 
                currentPage4: 当前页数
                pageSize4: 每页显示条数
                total4: 总条数
                handleSizeChange: 每页显示条目个数改变时触发
                handleCurrentChange: 当前页数改变时触发
             -->
            <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[3, 5, 7, 10,15, 20]"
                background layout="total, sizes, prev, pager, next, jumper" :total="total4"
                @size-change="handler" @current-change="getHasUser" />
        </footer>
    </el-card>
    
    <!-- 抽屉 添加/编辑用户 -->
    <el-drawer ref="drawerRef" v-model="dialog" :title="useradd_data.id ? '编辑用户' : '添加用户'" class="demo-drawer">
        <el-form ref="formRef" :model="useradd_data" :rules="rules" label-width="80px">
            <el-form-item label="用户姓名" prop="username">
                <el-input v-model="useradd_data.username" placeholder="请输入用户姓名"></el-input>
            </el-form-item>
            <el-form-item label="用户呢称" prop="fullname">
                <el-input v-model="useradd_data.fullname" placeholder="请输入用户呢称"></el-input>
            </el-form-item>
            <el-form-item label="用户密码" prop="pwd">
                <el-input type="password" v-model="useradd_data.pwd" placeholder="请输入用户密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-popconfirm width="220" confirm-button-text="OK" cancel-button-text="No, Thanks" icon="InfoFilled"
                    icon-color="#626AEF" title="是否确认添加该用户?" @confirm="handleUser">
                    <template #reference>
                        <el-button type="primary">提交</el-button>
                    </template>
                </el-popconfirm>

                <el-button type="danger">取消</el-button>
            </el-form-item>
        </el-form>
    </el-drawer>

    <!-- 抽屉 分配角色 -->
    <el-drawer v-model="dialog_role" title="分配角色" class="demo-drawer2">
        <el-form ref="formRef2" label-width="80px">
            <el-form-item label="用户名">
                <el-input v-model="role_data.UserName" disabled></el-input>
            </el-form-item>
            <el-form-item label="角色">
                <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate"
                    @change="handleCheckAllChange">全选</el-checkbox>
                <el-checkbox-group v-model="selectedRoles" @change="handleCheckedCitiesChange">
                    <el-checkbox v-for="role in roleNames" :key="role" :label="role" :value="role">{{ role }}</el-checkbox>
                </el-checkbox-group>
            </el-form-item>

            <el-form-item>
                <el-popconfirm width="220" confirm-button-text="OK" cancel-button-text="No, Thanks" icon="InfoFilled"
                    icon-color="#626AEF" title="是否确认给该用户分配角色?" @confirm="submitRole">
                    <template #reference>
                        <el-button type="primary">提交</el-button>
                    </template>
                </el-popconfirm>

                <el-button type="danger">取消</el-button>
            </el-form-item>
        </el-form>
    </el-drawer>

</template>

<style scoped lang="scss">
.form {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>